// 引入模块
const express = require("express");
const cors = require("cors");
const conn = require("./self/sql");

// 创建 web 服务
const app = express();
// 调用中间件
app.use(cors());
app.use(express.urlencoded());

// 查询接口
app.post("/user/search", (req, res) => {
    let {
        id,
        name
    } = req.body;
    console.log(id, name);
    let sql = null;
    if (id != undefined && id != "" && id != null) {
        sql = `select * from info where id=${id} `;
    } else if (name != undefined && name != "" && name != null) {
        sql = `select * from info where name like "%${name}%"`;
    } else {
        sql = `select * from info`;
    }
    console.log(sql);
    conn.query(sql, (err, result) => {
        if (err) {
            res.status(500).json({
                code: 500,
                msg: "查询失败"
            });
            return;
        }

        // console.log(result);
        // console.log(result.length);

        res.json({
            code: 200,
            msg: "查询成功",
            data: result
        });
    })
})

// 添加接口
app.post("/user/add", (req, res) => {
    let {
        name,
        sex,
        age
    } = req.body;
    // console.log(name, sex, age);

    const sql = `insert into info(name,sex,age) values ("${name}","${sex}",${age})`;
    console.log(sql);
    conn.query(sql, (err, result) => {
        if (err) {
            res.status(500).json({
                code: 500,
                msg: "添加失败"
            });
            return;
        }
        console.log(result.affectedRows);
        res.json({
            code: 200,
            msg: "添加成功"
        });
    })
})

// 修改接口
app.post("/user/update", (req, res) => {
    let {
        id,
        name,
        sex,
        age
    } = req.body;
    if (name == undefined || name == "" || name == null) {
        res.status(403).json({
            code: 403,
            msg: "姓名不能为空"
        });
        return;
    } else if (sex == undefined || sex == "" || sex == null) {
        res.status(403).json({
            code: 403,
            msg: "性别不能为空"
        });
        return;
    } else if (age == undefined || age == "" || age == null) {
        res.status(403).json({
            code: 403,
            msg: "年龄不能为空"
        });
        return;
    } else if (id == undefined || id == "" || id == null) {
        res.status(403).json({
            code: 403,
            msg: "id不能为空"
        });
        return;
    }

    let sql = `update info set name="${name}", sex="${sex}", age=${age} where id=${id}`;

    conn.query(sql, (err, result) => {
        if (err) {
            res.status(500).json({
                code: 500,
                msg: "更新失败"
            });
            return;
        }
        // console.log(result);
        if (result.affectedRows > 0) {
            res.json({
                code: 200,
                msg: "更新成功"
            });
        }
    })
})

// 删除接口
app.post("/user/del", (req, res) => {
    let {
        id
    } = req.body;
    let sql = "update info set isdelete=1 where id=" + id;

    conn.query(sql, (err, result) => {
        console.log(sql);
        if (err) {
            res.status(500).json({
                code: 500,
                msg: "删除失败"
            });
            return;
        }
        // console.log(result);
        if (result.affectedRows > 0) {
            res.json({
                code: 200,
                msg: "删除成功"
            });
        }
    })
})

// 监听 3000 端口
app.listen(3000, () => {
    console.log("ok");
})